package gui;

import gui.panels.CustomPanel;
import gui.panels.LocationPanel;
import gui.panels.MoviePanel;
import gui.panels.MusicPanel;
import gui.panels.PersonPanel;
import gui.panels.SeriesPanel;
import gui.tools.ActionHandler;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Frame extends JFrame {

	public JRadioButton rPersons, rMovies, rMusic, rSeries, rLocations, rCustom;
	public ButtonGroup bg;
	public PersonPanel ppan;
	public MoviePanel mpan;
	public MusicPanel mupan;
	public SeriesPanel sepan;
	public LocationPanel lopan;
	public CustomPanel cupan;
	private ActionHandler ac;
	public boolean isEnabled;

	public Frame() {
		setTitle("Movie Base v.01");
		setSize(350, 600);
		setResizable(false);
		setLayout(new BorderLayout());
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		ac = new ActionHandler(this);
		setJMenuBar(generateMenu(ac));
		ppan = new PersonPanel(ac);
		mpan = new MoviePanel(ac);
		mupan = new MusicPanel(ac);
		sepan = new SeriesPanel(ac);
		lopan = new LocationPanel(ac);
		cupan = new CustomPanel(ac);
		ac.getPersonAddtions(ppan);
		ac.getMovieAddtions(mpan);
		ac.getMusicAdditions(mupan);
		ac.getSeriesAdditions(sepan);
		ac.getLocationAdditions(lopan);
		add(radioGroup(ac), BorderLayout.NORTH);
		setVisible(true);
	}

	public JMenuBar generateMenu(ActionHandler ac) {
		JMenuBar mb = new JMenuBar();
		JMenu file = new JMenu("File");
		JMenuItem exit = new JMenuItem("Exit");
		JMenu chout = new JMenu("CheckOut");
		JMenuItem movie = new JMenuItem("Checkout Movie");
		JMenuItem music = new JMenuItem("Checkout Music");
		JMenuItem series = new JMenuItem("Checkout Series");
		exit.addActionListener(ac);
		movie.addActionListener(ac);
		music.addActionListener(ac);
		series.addActionListener(ac);
		JMenu sql = new JMenu("SQL");
		JMenuItem query1 = new JMenuItem("Query 1");
		query1.addActionListener(ac);
		JMenuItem query2 = new JMenuItem("Query 2");
		query2.addActionListener(ac);
		JMenuItem query3 = new JMenuItem("Query 3");
		query3.addActionListener(ac);
		JMenuItem query4 = new JMenuItem("Query 4");
		query4.addActionListener(ac);
		chout.add(movie);
		chout.add(music);
		chout.add(series);
		file.add(chout);
		file.add(exit);
		mb.add(file);
		sql.add(query1);
		sql.add(query2);
		sql.add(query3);
		sql.add(query4);
		sql.setEnabled(isEnabled);
		mb.add(sql);
		return mb;
	}
	
	public JPanel radioGroup(ActionHandler ac) {

		JPanel pn = new JPanel();
		pn.setLayout(new GridLayout(2, 3));

		rPersons = new JRadioButton("Persons");
		rMovies = new JRadioButton("Movies");
		rMusic = new JRadioButton("Music");
		rSeries = new JRadioButton("Series");
		rLocations = new JRadioButton("Locations");
		rCustom = new JRadioButton("Custom SQL");

		rPersons.addActionListener(ac);
		rMovies.addActionListener(ac);
		rMusic.addActionListener(ac);
		rSeries.addActionListener(ac);
		rLocations.addActionListener(ac);
		rCustom.addActionListener(ac);
		
		bg = new ButtonGroup();
		bg.add(rPersons);
		bg.add(rMovies);
		bg.add(rMusic);
		bg.add(rSeries);
		bg.add(rLocations);
		bg.add(rCustom);
		
		pn.add(rPersons);
		pn.add(rMovies);
		pn.add(rMusic);
		pn.add(rSeries);
		pn.add(rLocations);
		pn.add(rCustom);
		
		rPersons.setSelected(true);
		
		return pn;
	}
}
